﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Entidades.Reclamos;
using WebApplication.Reclamos;

public partial class frmMtoIncidencia : System.Web.UI.Page
{

    #region ATRIBUTOS

    protected IControllerRecIncidencia _incController;
    protected DataTable dt {
                    get{
                        if(ViewState["dt_Incidencia"]==null){
                            ViewState["dt_Incidencia"] = new DataTable();
                        }
                        return (DataTable)ViewState["dt_Incidencia"];
                    }
                    set{
                        ViewState["dt_Incidencia"] = value;
                    }
    }
    protected RecIncidencia criterio;

    #endregion

    #region CONSTRUCTOR

    public frmMtoIncidencia() {
        _incController = new ControllerRecIncidencia();
        criterio = new RecIncidencia();
    }

    #endregion

    #region EVENTOS DEL FORMULARIO

    protected void Page_Load(object sender, EventArgs e)
    {
        verMensajes();
        if (!IsPostBack) {
            //Listar(); 
        }
    }

    protected void verMensajes() {
        String msg = (Session["incidencia_rpta"] == null) ? "" : Session["incidencia_rpta"].ToString();        
        switch (msg) { 
            case "1":                
                Page.ClientScript.RegisterStartupScript(this.GetType(),"MSG_SAVE", "alert('" + Constantes.MSG_SAVE + "')", true);
                Session["incidencia_rpta"] = null;
                break;
            case "2":                
                Page.ClientScript.RegisterStartupScript(this.GetType(), "MSG_DELETE", "alert('" + Constantes.MSG_DELETE + "')", true);
                Session["incidencia_rpta"] = null;
                break;
            case "3":
                String msg_detail = (Session["incidencia_rpta_detail"] == null) ? "" : Session["incidencia_rpta_detail"].ToString();            
                msg_detail = msg_detail.Replace("\"","");
                msg_detail = msg_detail.Replace("'", "");
                msg_detail = msg_detail.Replace("\n", " ");
                msg_detail = msg_detail.Replace("\r", " ");
                Page.ClientScript.RegisterStartupScript(this.GetType(), "MSG_EXCEPTION1", "$('#mensaje').html('" + msg_detail + "'); ", true);
                //Page.ClientScript.RegisterStartupScript(this.GetType(), "MSG_EXCEPTION2", "$(document).ready(function(){tb_show('mensaje', '#TB_inline?height=240&amp;width=405&amp;inlineId=hiddenModalContent&amp;modal=true', null);}", true);
                Page.ClientScript.RegisterStartupScript(this.GetType(), "MSG_EXCEPTION2", "$(document).ready(function(){tb_show('" + Constantes.MSG_EXCEPTION +"', '#TB_inline?height=240&amp;width=405&amp;inlineId=hiddenModalContent&amp;modal=true', null);}", true);
                
                Session["incidencia_rpta"] = null;
                Session["incidencia_rpta_detail"] = null;


                break;
            default: 
                break;
        }        
    }

    protected void Listar() {
        List<RecIncidencia> lista = _incController.listarIncidencia(criterio);
        dt = new DataTable();
        dt.Columns.Add("Código");
        dt.Columns.Add("Area");
        dt.Columns.Add("Estado");
        dt.Columns.Add("Fecha");
        dt.Columns.Add("FechaSolucion");
        dt.Columns.Add("Cliente");
        dt.Columns.Add("Responsable");              

        DataRow dr;
        foreach (RecIncidencia be in lista)
        {
            dr = dt.NewRow();
            dr[0] = be.IdIncidencia.ToString();
            dr[1] = be.Area.ToString();
            dr[2] = (be.Estado.ToString()== Constantes.CODE_OPEN)?Constantes.STATUS_OPEN: Constantes.STATUS_CLOSE;
            dr[3] = be.FecIncidencia.ToString().Substring(0,10);
            dr[4] = be.FecSolucion.ToString().Substring(0, 10);
            dr[5] = be.Cliente.ToString();
            dr[6] = be.Responsable.ToString();            
            dt.Rows.Add(dr);
        }

        gvCargos.DataSource = dt;
        gvCargos.DataBind();
    }

    #endregion

    #region EVENTOS DE BOTONES

    protected void btnNuevo_Click(object sender, EventArgs e) {
        Session["incidencia_codigo"] = null;
        Session["incidencia_action"] = null;
        Session["incidencia_visible"] = null;
        Response.Redirect("frmIncidencia.aspx");
    }

    protected void btnBuscar_Click(object sender, EventArgs e)
    {
        criterio = new RecIncidencia();
        criterio.FecIncidencia = (txtFechaInicio.Text != "" ? DateTime.Parse(txtFechaInicio.Text) : DateTime.MinValue.Date);
        criterio.flIncidencia = ((txtFechaInicio.Text != "" ? DateTime.Parse(txtFechaInicio.Text) : DateTime.MinValue.Date) != DateTime.MinValue.Date);
        criterio.FecSolucion = (txtFechaFin.Text != "" ? DateTime.Parse(txtFechaFin.Text) : DateTime.MinValue.Date);
        criterio.flSolucion = ((txtFechaInicio.Text != "" ? DateTime.Parse(txtFechaInicio.Text) : DateTime.MinValue.Date) != DateTime.MinValue.Date);

        criterio.Estado = Int32.Parse(cmbEstado.SelectedValue);
        try
        {
            criterio.IdIncidencia = Int32.Parse(txtCodigo.Text);
        }
        catch {
            criterio.IdIncidencia = 0;
        }
        Listar(); 
    }

    #endregion

    #region EVENTOS DE GRILLA

    protected void gvCargos_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gvCargos.PageIndex = e.NewPageIndex;
        gvCargos.DataSource = dt;
        gvCargos.DataBind();
    }

    protected void verDetalle_OnClick(object sender, CommandEventArgs e)
    {
        string codigo = e.CommandName.ToString();
        Session["incidencia_codigo"] = codigo;
        Session["incidencia_action"] = "visualizar";
        Session["incidencia_visible"] = false;
        Response.Redirect("frmIncidencia.aspx");
    }

    protected void verModificar_OnClick(object sender, CommandEventArgs e)
    {
        string codigo = e.CommandName.ToString();
        Session["incidencia_codigo"] = codigo;
        Session["incidencia_action"] = "modificar";
        Session["incidencia_visible"] = true;
        Response.Redirect("frmIncidencia.aspx");
    }

    protected void verImprimir_OnClick(object sender, CommandEventArgs e)
    {
        string codigo = e.CommandName.ToString();
        Session["incidencia_codigo"] = codigo;
        Session["incidencia_action"] = "imprimir";
        Session["incidencia_visible"] = true;
        Page.ClientScript.RegisterStartupScript(this.GetType(), "DETALLE", "window.open('frmViewIncidencia.aspx','Detalle de incidencia','height=650,width=450'); ", true);
    }

    protected void verEliminar_OnClick(object sender, CommandEventArgs e)
    {
        int codigo = (e.CommandName==null)?0:Convert.ToInt32(e.CommandName.ToString());
        int resultado = 0;        
        String StrResultado = "";
        try
        {            
            resultado = _incController.eliminarIncidencia(codigo);
        }
        catch (Exception ex) {
            StrResultado = ex.Message.ToString();
        }

        if (StrResultado.Equals(""))
        {
            Session["incidencia_rpta"] = 2;
            Response.Redirect("frmMtoIncidencias.aspx");
        }
        else {
            Session["incidencia_rpta"] = 3;
            Session["incidencia_rpta_detail"] = StrResultado;
            Response.Redirect("frmMtoIncidencias.aspx");
        }
    }

    #endregion

    #region EVENTOS DEL CALENDARIO

    protected void SelectFecha(object sender, EventArgs e)
    {        
        try
        {
            Calendar obj = (Calendar)sender;
            txtFechaInicio.Text = obj.SelectedDate.ToString("MM/dd/yyyy");    
        }
        catch
        {

        }
    }

    protected void SelectFechaFin(object sender, EventArgs e)
    {      
        try
        {
            Calendar obj = (Calendar)sender;
            txtFechaFin.Text = obj.SelectedDate.ToString("MM/dd/yyyy");    
        }
        catch
        {

        }
    }

    #endregion

}